package com.pb.infra.taskManage.controller;

import com.pb.infra.oth.entity.common.DictVO;
import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.JwtTokenConfig;
import com.pb.infra.oth.util.SysLog;
import com.pb.infra.taskManage.entity.MaJobEventCfg;
import com.pb.infra.taskManage.service.TaskConService;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *任务配置
 * @author liaohaoyuan
 * @date 20210501
 */
@Slf4j
@RestController
@RequestMapping("v1/taskManage/taskCon")
public class TaskConController {

    /**
     * 业务逻辑对象
     */
    @Resource
    private TaskConService taskConService;

    /**
     * 日志操作表
     */
    private static final String TABLE_NAME = "ma_job_event_cfg";

    /**
     * 日志操作功能名
     */
    private static final String OPERATION_MENU = "任务配置";

    @Resource
    private JwtTokenConfig jwtTokenConfig;

    /**
     * 查询分页数据
     * @param paramMap 请求参数
     * @param page 页码
     * @return Result 返回数据
     */
    @GetMapping("select")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result select(@RequestParam Map<String, Object> paramMap, Page page) {
        return taskConService.select(paramMap, page);
    }
    /**
     * 新增
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("insert")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result insert(@RequestBody Map<String, Object> paramMap) {
        try {
            Result insert = taskConService.insert(paramMap);
            return insert;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(Constant.EXIST, Constant.INSERT_ERROR_MSG);
        }
    }
    /**
     * 修改
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("update")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result update(@RequestBody Map<String, Object> paramMap){
        try {
            Result update = taskConService.update(paramMap);
            return update;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(Constant.EXIST, Constant.UPDATE_ERROR_MSG);
        }
    }

    /**
     * 删除数据
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("delete")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DEL, menuName = OPERATION_MENU)
    public Result delete(@RequestBody List<Map<String,Object>> paramMap){
        taskConService.delete(paramMap);
        return Result.OK_EMPTY;
    }

    /**
     * 任务配置--导入
     *
     * @param paramMap               筛选条件
     * @param multipartHttpServletRequest 文件流
     */
    @PostMapping(value = "import")
    public Result taskConImport(@RequestParam Map<String, String> paramMap, MultipartHttpServletRequest multipartHttpServletRequest) {

        if (paramMap.get("groupName")==null||paramMap.get("groupName").trim().equals("")){
            return Result.error("600","缺少批次号");
        }
        if (paramMap.get("addType")==null||paramMap.get("addType").trim().equals("")){
            return Result.error("600","缺少添加模式");
        }
        MaJobEventCfg maJobEventCfg=new MaJobEventCfg();
        maJobEventCfg.setAddType(paramMap.get("addType"));
        maJobEventCfg.setGroupName(paramMap.get("groupName"));
        return this.taskConService.taskConImport(maJobEventCfg, multipartHttpServletRequest);
    }

    /*@PostMapping(value = "export")
    public void export(@RequestBody MaJobEventCfg maJobEventCfg, HttpServletResponse response) {
        this.taskConService.export(maJobEventCfg, response);
    }*/

    /**
     * 任务配置--导出
     *
     * @param maJobEventCfg 筛选条件
     */
    @PostMapping("export")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DOWN, menuName = OPERATION_MENU)
    public Map<String, Object> exportCodeExcel(@RequestBody  MaJobEventCfg maJobEventCfg, HttpServletRequest request){
        Map<String, Object> paramMap = new HashMap<>();
        String token = request.getHeader(jwtTokenConfig.getHeader());
        List<DictVO> dataList1 =  taskConService.getJobTypeDictList();
        List<DictVO> dataList2 =  taskConService.getCmdTypeDictList();
        List<DictVO> dataList3 =  taskConService.getJobFreqDictList();
        List<DictVO> dataList4 =  taskConService.getJobGroupDictList();
        List<MaJobEventCfg> dataList0 =  taskConService.queryList(maJobEventCfg);
        String[][] sheeHeader={{"任务ID", "任务名称", "任务类型", "命令类型", "执行内容", "运行频度", "分组"},
                {"名称", "编码"},{"名称", "编码"},{"名称", "编码"},{"名称", "编码"}};
        String[][] sheetColumn={{"jobId","jobName","jobType","cmdType","jobCmd","jobFreq","jobGroup"},
                {"label","value"},{"label","value"},{"label","value"},{"label","value"}};
        String[][] sheetComments={{"","","参考值查看任务类型sheet","参考值查看命令类型sheet","","参考值查看任务频度sheet","参考值查看分组sheet"},
                {"",""},{"",""},{"",""},{"",""}};
        String[][] dataType = {{"STRING","STRING","STRING","STRING","STRING","STRING","STRING"},
                {"STRING","STRING"},{"STRING","STRING"},{"STRING","STRING"},{"STRING","STRING"}};
        Object[] sheetData = {dataList0,dataList1,dataList2,dataList3,dataList4};
        String[] sheetName={"任务列表","任务类型","命令类型","任务频度","分组"};
        paramMap.put("jobBookName",sheetName);
        paramMap.put("title",sheeHeader);
        paramMap.put("order",sheetColumn);
        paramMap.put("comments",sheetComments);
        paramMap.put("data",sheetData);
        paramMap.put("token",token);
        paramMap.put("dataType",dataType);
        paramMap.put("fileName","任务列表");
        return  paramMap;
    }

}
